package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.PersonRoleMapper;
import com.dk.mapper.TPersonMapper;
import com.dk.pojo.PersonRoleExample;
import com.dk.pojo.TPerson;
import com.dk.pojo.TPersonExample;
import com.dk.service.PerosnService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class PerosnServiceImpl implements PerosnService {
    @Autowired
    private TPersonMapper tPersonMapper;
    @Autowired
    private PersonRoleMapper personRoleMapper;

    @Override
    public List<TPerson> getInfo(String pname) {
        TPersonExample tp = new TPersonExample();
        TPersonExample.Criteria criteria = tp.createCriteria();
        criteria.andPnameLike(pname);
        List<TPerson> list = tPersonMapper.selectByExample(null);
        List<TPerson> list1 = tPersonMapper.selectByExample(tp);
        return list1;

    }

    @Override
    @Transactional
    public int deleteById(Integer pid) {
        PersonRoleExample pe = new PersonRoleExample();
        PersonRoleExample.Criteria criteria = pe.createCriteria();
        criteria.andPersonIdEqualTo(pid);
        personRoleMapper.deleteByExample(pe);
        return tPersonMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public Boolean selectcheckPhone(String uname) {
       TPerson person = tPersonMapper.selectByName(uname);
        return person == null ? true:false;
    }

    @Override
    public TPerson selectByPname(String username) {
        return tPersonMapper.loginInfo(username);
    }

    @Override
    public TPerson loginInfo(TPerson user) {
        TPerson u = tPersonMapper.loginInfo(user.getPname());
        /*try {
            if (!MD5Utils.validPassword(user.getPwd(),u.getPwd())){
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        if(u == null){
            return null;
        }else {
            return u;
        }
    }



    @Override
    @Transactional
    public TPerson selectById(Integer id) {
        return tPersonMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateInfo(TPerson tPerson ,Integer[] roleId) {
        try {
            //修改用户表
            tPerson.setPwd("1234");
            tPersonMapper.updateByPrimaryKey(tPerson);
            //修改中间表
            PersonRoleExample pe = new PersonRoleExample();
            PersonRoleExample.Criteria criteria = pe.createCriteria();
            criteria.andPersonIdEqualTo(tPerson.getPid());
            personRoleMapper.deleteByExample(pe);
            //进行批量增加
            personRoleMapper.insertPidAndRid(tPerson.getPid(),roleId);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    @Transactional
    public int insert(TPerson tPerson,Integer[] rid) {
        try {
                tPerson.setPwd("1234");
               /* //初始密码123 用md5加密
                tPerson.setPwd(MD5Utils.getEncryptedPwd("123"));*/
                int i = tPersonMapper.insertReturnId(tPerson);
                Integer pid = tPerson.getPid();
                personRoleMapper.insertPidAndRid(pid,rid);
                return i;
        }catch (Exception e){
                e.printStackTrace();
                throw e;
        }

    }
}
